package com.demo2do.boyle.backend.web.controller.system;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.boyle.backend.service.system.MenuService;
import com.demo2do.boyle.common.entity.system.Menu;
import com.demo2do.boyle.common.entity.system.Role;
import com.demo2do.boyle.common.entity.system.RoleMenu;
import com.demo2do.core.entity.Result;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.interceptor.MenuSetting;

/**
 * 
 * @author Jason.Y
 *
 */
@Controller
@RequestMapping("/menus")
@MenuSetting("menu-system")
public class MenuController {
    
	@Autowired
	private MenuService menuService;
	
    /**
     * List all the menus
     * 
     * @return 
     */
    @RequestMapping("")
    @MenuSetting("submenu-system-principal-menu")
    public ModelAndView list() {
    	
        ModelAndView modelAndView = new ModelAndView("system/menu/role-list");
        
        List<Role> roles = menuService.list();
        modelAndView.addObject("rolesT", roles);
        
        return modelAndView;
    }
    
    /**
     * display the create panel
     * 
     * @return
     */
    @RequestMapping("/create")
    public ModelAndView create() {
        ModelAndView modelAndView = new ModelAndView("system/menu/role-create");
        return modelAndView;
    }
    
    /**
     * 
     * @return the create view
     */
    @RequestMapping("/loadSelectedMenu")
    public @ResponseBody String loadSelectedMenu() {
    	
    	return menuService.loadSelectedMenu();
    }
    
    /**
     * 
     * @return the create view
     */
    @RequestMapping("/tree_data2")
    public @ResponseBody String tree_data2() {
    	
    	StringBuffer sb = new StringBuffer();
    	sb.append("[{\"id\" : \"0\",\"text\" : \"测试Root节点\"}]");
    	
    	return sb.toString();
    }
    
    /**
     * 
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/saveRoleMenus")
    public String saveRoleMenus(@RequestParam(value = "description", required = false) String description,
					    		@RequestParam(value = "name", required = true) String name,
					    		@RequestParam(value = "menus", required = false) String menus,
					    		@RequestParam(value = "submenus", required = false) String submenus) {

        Result result = new Result();
        try {
        	
        	RoleMenu roleMenu = new RoleMenu();
        	roleMenu.setRoleName(name);
        	roleMenu.setMenus(menus);
        	roleMenu.setSubmenus(submenus);
            
        	Role role = new Role();
        	role.setName(name);
        	role.setAlias(name);
        	role.setDescription(description);
        	
        	roleMenu.initialize(role);
        	
            menuService.save(roleMenu);

            result.success().message("创建成功");

        } catch(Exception e) {
            result.fail().message("服务器异常");
        }

        return JsonUtils.toJsonString(result);
    }
    
    /**
     * display the edit panel
     * 
     * @param id
     * @return the variable info edit view
     */
    @RequestMapping("/showSelMenu")
    public ModelAndView showSelMenu() {
    	
        ModelAndView modelAndView = new ModelAndView("system/menu/menu-edit");
        
        String menuStr = menuService.showSelMenu();
        
        modelAndView.addObject("menuStr", menuStr);
        
        return modelAndView;
    }
    
    /**
     * display the edit panel
     * 
     * @param id
     * @return the variable info edit view
     */
    @RequestMapping("/{id}/edit")
    public ModelAndView edit(@PathVariable("id") Integer id) {
    	
        ModelAndView modelAndView = new ModelAndView("system/menu/menu-edit");
        
        Menu menu = menuService.load(Menu.class, id);
        
        modelAndView.addObject("menu", menu);
        
        return modelAndView;
    }
	
}
